package com.itrip.itripbiz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.itrip.common.DtoUtil;
import com.itrip.common.EmptyUtils;
import com.itrip.common.ValidationToken;
import com.itrip.dto.Dto;
import com.itrip.entity.ItripUser;
import com.itrip.entity.ItripUserLinkUser;
import com.itrip.service.ItripUserLinkUserService;
import com.itrip.vo.userinfo.ItripAddUserLinkUserVO;
import com.itrip.vo.userinfo.ItripSearchUserLinkUserVO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/biz/api")
public class HotelUserinfoControler {
    @Resource
    private ItripUserLinkUserService itripUserLinkUserService;
    @Resource
    private RedisTemplate redisTemplate;

    //查询用户联系人列表
    @PostMapping("/userinfo/queryuserlinkuser")
    public Dto<ItripUserLinkUser> queryUserLinkUser(@RequestBody ItripSearchUserLinkUserVO itripSearchUserLinkUserVO, HttpServletRequest request){

        String tokenString = request.getHeader("token");
        ItripUser itripUser = (ItripUser) redisTemplate.opsForValue().get(tokenString);
        if(EmptyUtils.isEmpty(itripUser)){
            return DtoUtil.returnFail("token失效，请重新登录","100000");
        }
        QueryWrapper qw = new QueryWrapper();
        qw.select("createdBy","creationDate","id","linkIdCard","linkIdCardType","linkPhone","linkUserName","modifiedBy","modifyDate","userId");
        if(itripSearchUserLinkUserVO.getLinkUserName() != null){
             qw.like("linkUserName",itripSearchUserLinkUserVO.getLinkUserName());
        }
        qw.eq("userId",itripUser.getId());
        List list = itripUserLinkUserService.list(qw);
        return DtoUtil.returnDataSuccess(list);

    }
    //添加常用联系人
    @PostMapping("/userinfo/adduserlinkuser")
    public Dto addUserLinkUser(@RequestBody ItripAddUserLinkUserVO itripAddUserLinkUserVO, HttpServletRequest request){
        String tokenString  = request.getHeader("token");
        ItripUser itripUser = (ItripUser) redisTemplate.opsForValue().get(tokenString);
        if(null != itripUser && null != itripAddUserLinkUserVO){
            ItripUserLinkUser itripUserLinkUser = new ItripUserLinkUser();
            itripUserLinkUser.setLinkUserName(itripAddUserLinkUserVO.getLinkUserName());
            itripUserLinkUser.setLinkIdCardType(itripAddUserLinkUserVO.getLinkIdCardType().longValue());
            itripUserLinkUser.setLinkIdCard(itripAddUserLinkUserVO.getLinkIdCard());
            itripUserLinkUser.setUserId(itripUser.getId());
            itripUserLinkUser.setLinkPhone(itripAddUserLinkUserVO.getLinkPhone());
            itripUserLinkUser.setCreatedBy(itripUser.getId());
            itripUserLinkUser.setCreationDate(new Date(System.currentTimeMillis()));
            try {
                itripUserLinkUserService.save(itripUserLinkUser);
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("新增常用联系人失败", "100411");
            }
            return DtoUtil.returnSuccess("新增常用联系人成功");
        }else if(null != itripUser && null == itripAddUserLinkUserVO){
            return DtoUtil.returnFail("不能提交空，请填写常用联系人信息","100412");
        }else{
            return DtoUtil.returnFail("token失效，请重新登录", "100000");
        }
    }
}
